package com.example.algorithm.no0414.solution;

import com.example.algorithm.no0414.Solution;

/**
 * @Description 解答-第三大的数-设置三个变量
 * @ClassName SolutionImpl
 * @Author Administrator
 * @Date 2020/5/20 10:26
 * @Version 1.0.0
 */
public class ThreeNumSolutionImpl implements Solution {

    private int MIN = Integer.MIN_VALUE;
    @Override
    public int thirdMax(int[] nums) {
        if(nums==null || nums.length==0){
            throw new RuntimeException("error");
        }
        int one = nums[0];
        int two = MIN;
        int three = MIN;

        for(int num : nums){
            if(num == one ||num==two || num==three){
                continue;
            }
            if(num > one){
                three = two;
                two = one;
                one = num;
            } else if(num > two){
                two = num;
                one = num;
            } else if(num > three) {
                three = num;
            }
        }

        if(three == MIN){
            return one;
        }

        return three;
    }
}
